--[[
FilePath: \Lua\View\UISoloBattle\UIUsingPotion.lua
1v1道具使用
--]]

require 'View/UIBase/UIBase'

UIUsingPotion = UIBase.New(PanelNames.UIUsingPotion)
local _M = UIUsingPotion

_M.hasInit = false
_M.modelId = 0;
_M.partnerId = "";
_M.selectPartnerId = "";
_M.isUse = false;

_M.POTION_REVIVE = 200621; --复活药水
_M.POTION_ENERGY = 200622; --能量药水
_M.POTION_HP = 200623;  --回复药水

function _M.Init(func, modelId, partnerId)
    _M.modelId = modelId;
    _M.partnerId = partnerId;
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self.btnClose = self:GetChild("Btn_BgClose");
        self.title = self:GetChild("Com_TwoBg"):GetChild("title");
        self.potion01 = self:GetChild("Btn_Potion01");
        self.potionNum01 = self.potion01:GetChild("Text_Num");
        self.potion02 = self:GetChild("Btn_Potion02");
        self.potionNum02 = self.potion02:GetChild("Text_Num");
        self.potion03 = self:GetChild("Btn_Potion03");
        self.potionNum03 = self.potion03:GetChild("Text_Num");
        self.tips = self:GetChild("Text_UsingTips");
        self.listHero = self:GetChild("List_Hero");
        self.btnUse = self:GetChild("Btn_Use");
        self.typeCtrl = self:GetController("Type");

        self.listHero.itemRenderer = self.refreshSelectItem;

        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()
    self.btnClose.onClick:Add(self.onClosePanel,self);
    self:RegisterEvent(Config.EventType.Fresh_RealManUseItem,self.realManUseItem);
    self:RegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeRealManUseItem);
end

function _M:removeEvent()
    self.btnClose.onClick:Remove(self.onClosePanel);
    self.btnUse.onClick:Clear();
    self:UnRegisterEvent(Config.EventType.Fresh_RealManUseItem,self.realManUseItem);
    self:UnRegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeRealManUseItem);
end

function _M:realManUseItem(isSuc, itemId)
    if not isSuc then
        self.isUse = false;
        return;
    end
    self:onClosePanel();
end

function _M:closeRealManUseItem()
    self:onClosePanel();
end

-- 显示
function _M:OnEnable()
    if self.modelId == nil or self.modelId == "" then
        self:onClosePanel();
    end
    self:refreshView();
end

function _M:refreshView()
    self.heroList = {};
    self.realManPlayerInfo = logicMgr.RealManWarManager.GetRealManPlayerInfo();
    if self.modelId == self.POTION_REVIVE then
        if self.realManPlayerInfo ~= nil and self.realManPlayerInfo.formation ~= nil and self.realManPlayerInfo.myTeam ~= nil then
            local battleNum = #self.realManPlayerInfo.formation;
            for i = 1, battleNum do
                local parId = self.realManPlayerInfo.formation[i];
                local myTeam = self.realManPlayerInfo.myTeam[parId];
                if myTeam ~= nil and myTeam.hp == 0 then
                    local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(parId, false);
                    if hero ~= nil then
                        table.insert(self.heroList, hero);
                    end
                end
            end
        end
    else
        if self.partnerId ~= nil and self.partnerId ~= "" then
            local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(self.partnerId, false);
            if hero ~= nil then
                table.insert(self.heroList, hero);
            end
        end
    end
    if self.modelId == self.POTION_REVIVE then
        self:updateRevive();
    elseif self.modelId == self.POTION_ENERGY then
        self:updateEnergy();
    elseif self.modelId == self.POTION_HP then
        self:updateHp();
    end
    self.listHero.numItems = #self.heroList;
end

function _M:updateRevive()
    self.typeCtrl.selectedIndex = 0;
    self.title.text = getLanguage("SoloBattle_PotionName01");
    UIComponentInterface:SetItemAllById(self.potion01:GetChild("Loader_Type"), nil, nil, self.POTION_REVIVE);
    self.tips.text = getLanguage("SoloBattle_UsingTips01");
    local num = 0;
    if self.realManPlayerInfo ~= nil and self.realManPlayerInfo.potion ~= nil and self.realManPlayerInfo.potion[self.POTION_REVIVE] ~= nil and self.realManPlayerInfo.potion[self.POTION_REVIVE] > 0 then
        num = self.realManPlayerInfo.potion[self.POTION_REVIVE];
    end
    self.potionNum01.text = tostring(num);
    self.btnUse.onClick:Clear();
    self.btnUse.onClick:Add(function()
        if not logicMgr.RealManWarManager.isOpen() then
            UISysTips.AddMsg(getLanguage("ErrorCode_200007"));
            return;
        end
        if num > 0 then
            if self.heroList == nil or #self.heroList < 1 or self.selectPartnerId == nil or self.selectPartnerId == "" then
                UISysTips.AddMsg(getLanguage("ErrorCode_200005"));
            else
                if self.isUse then
                    return;
                end
                self.isUse = true;
                RealManWarHandler.sendReqRealManFuckUseItemMessage(self.selectPartnerId, self.POTION_REVIVE);
            end
        else
            UISysTips.AddMsg(getLanguage("ErrorCode_200002"));
        end
    end);
end

function _M:updateEnergy()
    self.typeCtrl.selectedIndex = 1;
    self.title.text = getLanguage("SoloBattle_PotionName02");
    UIComponentInterface:SetItemAllById(self.potion02:GetChild("Loader_Type"), nil, nil, self.POTION_ENERGY);
    self.tips.text = getLanguage("SoloBattle_UsingTips02");
    local num = 0;
    if self.realManPlayerInfo ~= nil and self.realManPlayerInfo.potion ~= nil and self.realManPlayerInfo.potion[self.POTION_ENERGY] ~= nil and self.realManPlayerInfo.potion[self.POTION_ENERGY] > 0 then
        num = self.realManPlayerInfo.potion[self.POTION_ENERGY];
    end
    self.potionNum02.text = tostring(num);
    self.btnUse.onClick:Clear();
    self.btnUse.onClick:Add(function()
        if not logicMgr.RealManWarManager.isOpen() then
            UISysTips.AddMsg(getLanguage("ErrorCode_200007"));
            return;
        end
        if num > 0 then
            if self.heroList == nil or #self.heroList < 1 then
                UISysTips.AddMsg(getLanguage("ErrorCode_200008"));
            else
                local team = self.realManPlayerInfo.myTeam[tostring(self.partnerId)];
                if team == nil or ((team.hp == 100 or team.hp == -1) and team.energy == 1000) then
                    UISysTips.AddMsg(getLanguage("ErrorCode_200004"));
                else
                    if self.isUse then
                        return;
                    end
                    self.isUse = true;
                    RealManWarHandler.sendReqRealManFuckUseItemMessage(self.partnerId, self.POTION_ENERGY);
                end
            end
        else
            UISysTips.AddMsg(getLanguage("ErrorCode_200002"));
        end
    end);
end

function _M:updateHp()
    self.typeCtrl.selectedIndex = 2;
    self.title.text = getLanguage("SoloBattle_PotionName03");
    UIComponentInterface:SetItemAllById(self.potion03:GetChild("Loader_Type"), nil, nil, self.POTION_HP);
    self.tips.text = getLanguage("SoloBattle_UsingTips03");
    local num = 0;
    if self.realManPlayerInfo ~= nil and self.realManPlayerInfo.potion ~= nil and self.realManPlayerInfo.potion[self.POTION_HP] ~= nil and self.realManPlayerInfo.potion[self.POTION_HP] > 0 then
        num = self.realManPlayerInfo.potion[self.POTION_HP];
    end
    self.potionNum03.text = tostring(num);
    self.btnUse.onClick:Clear();
    self.btnUse.onClick:Add(function()
        if not logicMgr.RealManWarManager.isOpen() then
            UISysTips.AddMsg(getLanguage("ErrorCode_200007"));
            return;
        end
        if num > 0 then
            if self.heroList == nil or #self.heroList < 1 then
                UISysTips.AddMsg(getLanguage("ErrorCode_200008"));
            else
                local team = self.realManPlayerInfo.myTeam[tostring(self.partnerId)];
                if team == nil or team.hp == 100 or team.hp == -1 then
                    UISysTips.AddMsg(getLanguage("ErrorCode_200004"));
                else
                    if self.isUse then
                        return;
                    end
                    self.isUse = true;
                    RealManWarHandler.sendReqRealManFuckUseItemMessage(self.partnerId, self.POTION_HP);
                end
            end
        else
            UISysTips.AddMsg(getLanguage("ErrorCode_200002"));
        end
    end);
end

function _M.refreshSelectItem(index, itemObj)
    local hero = _M.heroList[index + 1];
    if hero == nil then
        itemObj.visible = false;
        return ;
    end
    local charactor = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    if charactor ~= nil then
        charactor.initData(itemObj, hero);
        charactor.updateDecTitle(itemObj, hero, 0, nil, EnumConst.FightTypeConst.RealMan);
    end
    local ctrlHadBattle = itemObj:GetController("ctrlHadBattle");
    local herodead = itemObj:GetController("herodead");
    local ctrlBar = itemObj:GetController("Bar");
    local hpBar = itemObj:GetChild("BloodBar");
    local mpBar = itemObj:GetChild("EnergyBar");

    ctrlHadBattle.selectedIndex = 0;
    itemObj.onClick:Clear();
    local isDead = false;
    local hp = 100;
    local mp = 100;
    herodead.selectedIndex = 0;
    local myHero = _M.realManPlayerInfo.myTeam[hero.partnerId];
    if myHero ~= nil and myHero.hp == 0 then
        herodead.selectedIndex = 1;
        isDead = true;
        hp = 0;
        mp = 0;
    else
        if myHero == nil then
            hp = 100;
            mp = 100;
        else
            hp = math.min(myHero.hp / myHero.maxHp * 100, 100);
            mp = math.min(tonumber(myHero.energy) / 1000 * 100, 100);
        end
    end

    ctrlBar.selectedIndex = 1
    hpBar.value = hp
    mpBar.value = mp
    if _M.modelId == _M.POTION_REVIVE then
        if index == 0 then
            _M.ctrlHadBattle = ctrlHadBattle;
            ctrlHadBattle.selectedIndex = 1;
            _M.selectPartnerId = hero.partnerId;
        else
            ctrlHadBattle.selectedIndex = 0;
        end
        itemObj.onClick:Add(function()
            if hero.partnerId ~= _M.selectPartnerId then
                _M.selectPartnerId = hero.partnerId;
                if _M.ctrlHadBattle ~= nil then
                    _M.ctrlHadBattle.selectedIndex = 0;
                    _M.ctrlHadBattle = ctrlHadBattle;
                    _M.ctrlHadBattle.selectedIndex = 1;
                end
            end
        end);
    end
end

function _M:onClosePanel()
    closeUI(PanelNames.UIUsingPotion);
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self.hasInit = false;
    self.modelId = 0;
    self.partnerId = "";
    self.selectPartnerId = "";
    self.isUse = false;
end

return _M;